package com.kingsoft.dc.khaos.deploy

import com.kingsoft.dc.khaos.util.{KhaosConstants, KhaosUtils, Logging}
import com.kingsoft.dc.khaos.{KhaosConf, KhaosContext}
import scopt.OptionParser

/**
  * Created by chengguangqing on 2019/5/27.
  */
object AppLaunch extends Logging {
  private[khaos] var exitFn: Int => Unit = (exitCode: Int) => System.exit(exitCode)

  def main(args: Array[String]): Unit = {
    try {
      //读取配置命令行参数
//      val defaultParams = new AppParams()
//      val parser = new OptionParser[AppParams]("khaos 算法组件") {
//        head("khaos 算法组件")
//        opt[String]("userConfig")
//          .text("dag配置信息")
//          .required()
//          .action((x, c) => c.copy(userConfig = x))
//      }

      //add 2019/8/30
//      val params = parser.parse(args, defaultParams).get
//      val userConfigJson: String = params.userConfig
//      val decodedUserConfig = KhaosUtils.decodeBase64Json(userConfigJson)

      // add 2020/10/27 读配置文件解决参数过长问题
      val userConfigMap = KhaosUtils.loadDefaultConf(KhaosConstants.KHAOS_USER_CONF)
      val userConfigVal = userConfigMap.get("userConfig").get
      val decodedUserConfig = KhaosUtils.decodeBase64Json(userConfigVal)


      //初始化配置
      val khaosConf = new KhaosConf()
      //add 2019/8/30
      khaosConf.set("khaos.app.json", decodedUserConfig)

      // 输出用户配置中的Json
      if (true) {
        log.info("------------Json begin-----------")
        log.info(decodedUserConfig)
        log.info("------------Json end-------------")
      }

      val kc = new KhaosContext(khaosConf)
      kc.runApp()
      //exitFn(0)
    } catch {
      case e: Exception =>
        e.printStackTrace(); //exitFn(-1)
        throw e
      case e: Throwable =>
        e.printStackTrace(); //exitFn(-1)
        throw e
    }
  }
}
